<?php

namespace Admin\Controller;
use Admin\Api\DatatreeApi;
use Admin\Api\PostApi;
use Admin\Api\VPostInfoApi;
use Admin\Model\DatatreeModel;
use Uclient\Api\UserApi;


class HelpCenterController extends AdminController {
    public function index(){
        $result  = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>'6081')));
        if($result['status']){
            $this->assign("article_cates",$result['info']);//分类名称列出
        }


        $name = I("name",'');
        $cate = I("cate",'');

        //分页时带参数get参数
        $params = array(
            'name'=>$name,
        );
        if($result['info']){
            $map = array();

            $map['post_category'] =array('in',implode(',',array_column($result['info'],"id")));

//		$map['post_author'] = UID;
            $map['name'] = array('like', '%'.$name.'%');
            if(!empty($cate)){
                $map['post_category'] = $cate;
                $params['cate'] = $cate;
                $this->assign("cur_cate",$cate);
            }

            $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
            $order = " post_modified desc ";

            $result = apiCall(VPostInfoApi::QUERY,array($map,$page,$order,$params));

            //
            if($result['status']){
                $this->assign('name',$name);
                $this->assign('show',$result['info']['show']);
                $this->assign('list',$result['info']['list']);//文章信息
            }else{
                LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
                $this->error(L('UNKNOWN_ERR'));
            }
        }
        $this->display();

    }

    function add(){
        if(IS_GET){

            $this->display();
        }else{
            $post_category = I('post.post_category',20);
            $start_time = I('post.start_time',0);
            $end_time = I('post.end_time',0);
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $jump_url = I('post.jump_url','');

            $user = session("global_user");

            $entity = array(
                'jump_url'=>$jump_url,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'main_img'=>I('post.main_img',''),
                'post_category'=>$post_category,
                'post_content'=>I('post_content',''),
                'post_excerpt'=>I('post_excerpt',''),
                'post_title'=>I('post_title',''),
                'post_author'=>UID,
                'post_status'=>I('post_status','draft'),
                'comment_status'=>I('commen_status','closed'),
                'post_parent'=>0,
                'post_type'=>'post_type',
                'comment_count'=>0
            );

            $entity['post_author_name'] = $user['username'];
            $result = apiCall("Admin/Post/add", array($entity));
            if(!$result['status']){
                $this->error($result['info']);
            }

            $this->success("操作成功！",U('Admin/HelpCenter/index'));

        }

    }

    public function edit(){
        $id = I('id',0);
        if(IS_GET){
            $result = apiCall("Admin/Post/getInfo", array(array("id"=>$id)));
            if(!$result['info']){
                $this->error($result['info']);
            }
            $this->assign("vo",$result['info']);
            $this->display();
        }else{
            $post_category = I('post.post_category',20);

            $start_time = I('post.start_time','');
            $end_time = I('post.end_time','');
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $user = session("global_user");
            $jump_url = I('post.jump_url','');

            $entity = array(
                'jump_url'=>$jump_url,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'main_img'=>I('post.main_img',''),
                'post_category'=>$post_category,
                'post_author'=>UID,
                'post_content'=>I('post_content',''),
                'post_excerpt'=>I('post_excerpt',''),
                'post_title'=>I('post_title',''),
                'post_status'=>I('post_status','draft'),
                'comment_status'=>I('commen_status','closed'),
            );
            $entity['post_author_name'] = $user['username'];
            $result = apiCall(PostApi::SAVE_BY_ID, array($id,$entity));

            if(!$result['status']){
                $this->error($result['info']);
            }

            $this->success("保存成功！",U('Admin/HelpCenter/index'));

        }
    }

    public function delete(){
        $id = I('id',0);

        $result = apiCall("Admin/Post/delete", array(array("id"=>$id)));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->success("删除成功！");

    }
}